﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DocumentFormat.OpenXml.Packaging;

namespace Exentials.Xoox.Spreadsheet
{
    public class XSheets : XElementSequenceContainer<XSheet>
    {
        private int sheetCount;
        private XWorkbook _workbook;

        public XSheets(XWorkbook workbook)
            : base("sheets")
        {
            _workbook = workbook;
            sheetCount = 0;
        }

        public XWorksheet AddWorksheet(string name)
        {
            string relationId = _workbook.GetNewId();
            WorksheetPart workSheetPart = _workbook.DocumentPart.AddNewPart<WorksheetPart>(relationId);

            XWorksheet xWorksheet = new XWorksheet(workSheetPart) { Workbook = _workbook };

            XSheet xsheet = new XSheet(name, ++sheetCount, relationId, xWorksheet);
            base.Add(xsheet);

            return xWorksheet;
        }

        internal void Save()
        {
            foreach (var sheet in this)
            {
                sheet.WorkSheet.Save();
            }
        }
    }
}
